//
// Created by xili on 2024/8/2 19:40.
// Go big or go home.
//
#include <vector>
#include <unordered_map>

using namespace std;

class Solution {
public:
    int subarraySum(vector<int> &nums, int k) {
        unordered_map<int, int> m;
        m.emplace(0, 1);
        int n = nums.size();
        int sum = 0, ans = 0;
        for (int i = 0; i < n; i++) {
            sum += nums[i];
            ans += m[sum - k];
            m[sum]++;
        }
        return ans;
    }
};